//
//  NotLoginView.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/4/20.
//  Copyright © 2016年 syf. All rights reserved.
//


//     let width = UIScreen.mainScreen().bounds.size.width
//     let height = UIScreen.mainScreen().bounds.size.height


import UIKit
// 声明协议
protocol NotLoginDelegate : NSObjectProtocol{
    func userRegisterClick()
    func userLoginingClick()
}

class NotLoginView: UIView {
    // weak 防止循环引用
    weak var delegate : NotLoginDelegate?
    
    override init(frame: CGRect) {
     super.init(frame: frame)
       backgroundColor = UIColor.whiteColor()
        createUI()
       // startAnimation()
    }

    
    required init?(coder aDecoder: NSCoder) {
      fatalError("coder method not implemented")
    }
    
    //MARK: - 开始动画
    private func startAnimation() {
     let anim =  CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    func showDifferentText(ISHome: Bool, imgStr: String, alertStr: String){
    
      iconView.hidden = !ISHome
      homeIconView.image = UIImage(named: imgStr)
      messageLabel.text = alertStr;
        if ISHome{
        startAnimation()
        }
    }
    
    
    
    private func createUI() {
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
     loginButton.setBackgroundImage(adjustIMg("common_button_white_disable"), forState: UIControlState.Normal)
    registerButton.setBackgroundImage(adjustIMg("common_button_white_disable"), forState: UIControlState.Normal)
        
        
        // 2.1背景图标
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        // 2.2遮罩
        maskIconView.xmg_Fill(self)
        // 2.3小房子
        homeIconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        // 2.4消息文字
        messageLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        // 2.5注册按钮
        registerButton.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 35), offset: CGPoint(x: 0, y: 20))
        // 2.6登录按钮
        loginButton.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 35), offset: CGPoint(x: 0, y: 20))
    }
    
    
    private lazy var iconView : UIImageView = {
        let img = UIImageView(image:  UIImage(named: "visitordiscover_feed_image_smallicon") )
        return img
    }()
    
    /// 遮罩视图
    // 注意系统有一个叫做maskView的属性, 属性名称不能叫做maskView
    private lazy var maskIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return iv
    }()
    
    /// 小房子
    private lazy var homeIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    
    /// 消息文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
         btn.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return btn
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
       btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return btn
    }()
    
    
     //MARK: - 对图片进行中心拉伸
    func adjustIMg(imgStr: String) ->UIImage {
        let img = UIImage(named: imgStr)
        let  w = img!.size.width * 0.5
        let  h = img!.size.height * 0.5
        
        return img!.resizableImageWithCapInsets(UIEdgeInsets(top: h, left: w, bottom: h, right: w))
    }
    
    //MARK: - 登录
     func loginClick() {
    delegate?.userLoginingClick()
    }
    
    //MARK: - 注册
     func registerClick() {
        delegate?.userRegisterClick()
    }

}
